// pages/typegoods/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlService: app.urlService,
    contentH: 0,
    imageH: 0,
    searchVal: "",
    data: null,
    catgeory: null,
    catId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      catId: options.catId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    var that = this;
    wx.createSelectorQuery().select('.head').boundingClientRect().exec(function (hres) {
      that.setData({
        contentH: winHeight - hres[0].height - 35
      })
    });
    this.bindCategoryList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindCategoryList: function () {
    var that = this;
    wx.request({
      url: this.data.urlService + '/api/Good/GetAllCategories',
      method: "POST",
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (res) {
        var data = JSON.parse(res.data);
        if (data.status) {
          var catgeory = data.data;
          for (var i = 0; i < catgeory.length; i++) {
            if (i == 0 && (that.data.catId == 0 || that.data.catId == null)) {
              catgeory[i].selected = true;
              that.setData({
                catId: catgeory[i].id
              })
              break;
            }
            if (catgeory[i].id == that.data.catId) {
              catgeory[i].selected = true;
              that.setData({
                catId: catgeory[i].id
              })
              break;
            }
          }
          that.setData({
            catgeory: catgeory
          })
          that.bindList();
        }
      }
    })
  }, 
  bindList: function () {
    var that = this;
    var whereCat = "";
    if (this.data.catId != 0 && this.data.catId != null) {
      whereCat = ",\"catId\":" + this.data.catId + "";
    }
    var where = "{\"searchName\":\"" + that.data.searchVal + "\"" + whereCat + "}";
    wx.request({
      url: this.data.urlService + '/api/Good/GetGoodsPageList',
      method: "POST",
      data: {
        page: 1,
        limit: 15,
        where: where
      },
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (res) {
        var data = JSON.parse(res.data);
        if (data.status) {
          that.setData({
            data: data.data.list,
          })
        }
        wx.createSelectorQuery().select('.item-img').boundingClientRect().exec(function (hres) {
          if (hres[0] != null) {
            that.setData({
              imageH: hres[0].width
            })
          }
        });
      }
    })
  },
  setSearchVal: function (e) {
    this.setData({
      searchVal: e.detail.value
    })
  },
  search: function () {
    this.bindList();
  },
  changeCat: function (e) {
    var catId = e.currentTarget.dataset.id;
    this.setData({
      catId: catId
    })
    var catgeory = this.data.catgeory;
    for (var i = 0; i < catgeory.length; i++) {
      if (catgeory[i].id == catId) {
        catgeory[i].selected = true;
      } else {
        catgeory[i].selected = false;
      }
    }
    this.setData({
      catgeory: catgeory
    })
    this.bindList();
  },
  godetail:function(e){
    var goodid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../gooddetail/index?id=" + goodid + ""
    })
  }
})